package org.dreamwork.assistant.tools.network.gui;

import org.apache.log4j.Logger;

/**
 * Created by seth.yang on 2018/5/7
 */
public abstract class NetworkReader implements Runnable {
    private String name;
//    IMessageLogger logger;
    protected NetworkReaderListener listener;

    private final Logger log = Logger.getLogger (NetworkReader.class);

    protected abstract void doWork ();
    public abstract void shutdown ();

    NetworkReader (/*IMessageLogger logger, */String name) {
//        this.logger = logger;
        this.name   = name;
    }

    public void setListener (NetworkReaderListener listener) {
        this.listener = listener;
    }

    @Override
    public void run () {
        Thread.currentThread ().setName (name);
        log.info (name + " worker stated.");
        try {
            doWork ();
        } catch (Exception ex) {
            log.warn (ex.getMessage (), ex);
        }

        log.info (name + " worker done!");
        if (listener != null) {
            listener.onClosed ();
        }
    }

    public interface NetworkReaderListener {
        void onClosed ();
        void onReceived (byte[] data);
    }
}